﻿using Business.Base;
using Business.Common;
using Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.ScrewPump.Perf
{

    public class ScrewPumpPerfExperimentDBManager : IDBManager
    {
        private String fields = "experimentCode,  designFlow, designPresure,    designShaftPower, designPumpEfficiency,   designNPSH, dInlet,  dOutlet,  designStages,  designRatedSpeed,  designSynchronousSpeed, designRatedVoltage,    designRatedCurrent,  designWindingConnection, designWindingMaterial, currentTransformationRatio, motorEfficiency, zInlet, zOutlet, lInlet, lOutlet ,kInlet,kOutlet,flowmeter,flowmeterAddress,experimentResultFlow,experimentResultPresure,experimentResultPower,experimentResultEff,experimentResultFlowIsPass,experimentResultPresureIsPass,experimentResultPowerIsPass,experimentResultEffIsPass";
        private String values = "@experimentCode, @designFlow, @designPresure, @designShaftPower, @designPumpEfficiency, @designNPSH, @dInlet, @dOutlet, @designStages, @designRatedSpeed, @designSynchronousSpeed, @designRatedVoltage, @designRatedCurrent, @designWindingConnection, @designWindingMaterial,@currentTransformationRatio,@motorEfficiency,@zInlet,@zOutlet,@lInlet,@lOutlet,@kInlet,@kOutlet,@flowmeter,@flowmeterAddress,@experimentResultFlow,@experimentResultPresure,@experimentResultPower,@experimentResultEff,@experimentResultFlowIsPass,@experimentResultPresureIsPass,@experimentResultPowerIsPass,@experimentResultEffIsPass";


        private String table = "ScrewPump_Perf_Experiment";
        SqlConnection connection = null;

        private SqlCommand sqlSelectCountCommand = null;
        private SqlCommand sqlSelectCommand = null;
        private SqlDataAdapter sqlSelectAdapter = null;
        private SqlCommand sqlInsertCommand = null;

        private SqlCommand sqlDeleteCommand = null;

        public ScrewPumpPerfExperimentDBManager()
        {
            String connectionString = DataSourceConfig.DataSourceString;
            connection = new SqlConnection(connectionString);

            sqlSelectCountCommand = new SqlCommand();
            sqlSelectCountCommand.Connection = connection;

            sqlSelectCommand = new SqlCommand();
            sqlSelectCommand.Connection = connection;

            sqlInsertCommand = new SqlCommand();
            sqlInsertCommand.Connection = this.connection;
            sqlInsertCommand.CommandText = "INSERT INTO [" + table + "] (" + fields + ") VALUES (" + values + ")";

            sqlDeleteCommand = new SqlCommand();
            sqlDeleteCommand.Connection = this.connection;
            sqlDeleteCommand.CommandText = "delete from " + table;

            sqlSelectAdapter = new SqlDataAdapter();
            sqlSelectAdapter.SelectCommand = sqlSelectCommand;
        }



        public int GetCount(BaseQuery query)
        {
            throw new NotImplementedException();
        }

        public DataTable GetDataList(BaseQuery query)
        {
            throw new NotImplementedException();
        }

        public int Insert(object obj)
        {
            ScrewPumpPerfExperiment screwPumpPerfExperiment = (ScrewPumpPerfExperiment)obj;
            sqlInsertCommand.Parameters.Clear();
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentCode", screwPumpPerfExperiment.Code));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designFlow", screwPumpPerfExperiment.DesignFlow));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designPresure", screwPumpPerfExperiment.DesignPressure));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designShaftPower", screwPumpPerfExperiment.DesignShaftPower));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designPumpEfficiency", screwPumpPerfExperiment.DesignPumpEfficiency));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designNPSH", screwPumpPerfExperiment.DesignNPSH));
            //sqlInsertCommand.Parameters.Add(new SqlParameter("@designInlet", perfExperiment.DesignInlet));
            //sqlInsertCommand.Parameters.Add(new SqlParameter("@designOutlet", screwPumpPerfExperiment.DesignOutlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designStages", screwPumpPerfExperiment.DesignStages));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designRatedSpeed", screwPumpPerfExperiment.DesignRatedSpeed));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designSynchronousSpeed", screwPumpPerfExperiment.DesignSynchronousSpeed));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designRatedVoltage", screwPumpPerfExperiment.DesignRatedVoltage));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designRatedCurrent", screwPumpPerfExperiment.DesignRatedCurrent));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designWindingConnection", screwPumpPerfExperiment.DesignWindingConnection));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@designWindingMaterial", screwPumpPerfExperiment.DesignWindingMaterial));
            //sqlInsertCommand.Parameters.Add(new SqlParameter("@designBenchmarkWorkingTemprature", screwPumpPerfExperiment.DesignBenchmarkWorkingTemprature));
           // sqlInsertCommand.Parameters.Add(new SqlParameter("@designPolarLogarithm", screwPumpPerfExperiment.DesignPolarLogarithm));
            //sqlInsertCommand.Parameters.Add(new SqlParameter("@designStrayLoss", screwPumpPerfExperiment.DesignStrayLoss));

            sqlInsertCommand.Parameters.Add(new SqlParameter("@currentTransformationRatio", screwPumpPerfExperiment.CurrentTransformationRatio));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@motorEfficiency", screwPumpPerfExperiment.MotorEfficiency));

            sqlInsertCommand.Parameters.Add(new SqlParameter("@dInlet", screwPumpPerfExperiment.DInlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@dOutlet", screwPumpPerfExperiment.DOutlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@zInlet", screwPumpPerfExperiment.ZInlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@zOutlet", screwPumpPerfExperiment.ZOutlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@lInlet", screwPumpPerfExperiment.LInlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@lOutlet", screwPumpPerfExperiment.LOutlet));

            sqlInsertCommand.Parameters.Add(new SqlParameter("@kInlet", screwPumpPerfExperiment.KInlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@kOutlet", screwPumpPerfExperiment.KOutlet));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@flowmeter", screwPumpPerfExperiment.Flowmeter));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@flowmeterAddress", screwPumpPerfExperiment.FlowmeterAddress));

            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultFlow", screwPumpPerfExperiment.ExperimentResultFlow));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultPresure", screwPumpPerfExperiment.ExperimentResultPressure));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultPower", screwPumpPerfExperiment.ExperimentResultPower));

            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultEff", screwPumpPerfExperiment.ExperimentResultEff));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultFlowIsPass", screwPumpPerfExperiment.ExperimentResultFlowIsPass));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultPresureIsPass", screwPumpPerfExperiment.ExperimentResultPressureIsPass));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultPowerIsPass", screwPumpPerfExperiment.ExperimentResultPowerIsPass));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentResultEffIsPass", screwPumpPerfExperiment.ExperimentResultEffIsPass));



            connection.Open();
           int id = (int)  sqlInsertCommand.ExecuteScalar();
            connection.Close();
            return id;
        }

        public void Delete(Object id)
        {
            if (id != null)
            {
          this.sqlDeleteCommand.CommandText = "delete from " + table + " where experimentCode = @code";
                sqlDeleteCommand.Parameters.Add(new SqlParameter("@code", (string)id));
            }
            connection.Open();
            sqlDeleteCommand.ExecuteNonQuery();
            sqlDeleteCommand.Parameters.Clear();
            connection.Close();
        }


        public void DeleteByCondtion(string condition)
        {
            this.sqlDeleteCommand.CommandText = "delete from " + table + condition;
            connection.Open();
            sqlDeleteCommand.ExecuteNonQuery();
            sqlDeleteCommand.Parameters.Clear();
            connection.Close();
        }
    }
}
